package neusoft.edu.cn.mysanjixiangmu527hdl.adapter;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import neusoft.edu.cn.mysanjixiangmu527hdl.R;
import neusoft.edu.cn.mysanjixiangmu527hdl.bean.ArticleBean;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Thinkpad on 2017/5/11.
 */

public class MyAdapter1 extends RecyclerView.Adapter {
    private Context context;
    private LayoutInflater layoutInflater;//动态加载布局
    private List<ArticleBean> list;//保存要显示的数据
    //1.自定义：构造方法，传进上下文
    public MyAdapter1(Context context) {
        this.context=context;
        layoutInflater=LayoutInflater.from(context);
    }
    //2.自定义：获取数据
    public void setList(List<ArticleBean> list){
        this.list=list;
    }
    //3.自定义：viewHolder子类
    public class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView tv_title,tv_descript,tv_time;
        private Button button;
        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.imageView);
            tv_title = (TextView) itemView.findViewById(R.id.textView);
            tv_descript = (TextView) itemView.findViewById(R.id.textView2);
            tv_time = (TextView) itemView.findViewById(R.id.textView3);
            button = (Button) itemView.findViewById(R.id.button);
        }
    }
    //4重写:生成item的View
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflate第一个参数要加载的布局id，第二个参数是指给该布局的外部再嵌套一层父布局，如果不需要就直接传null
        View v = layoutInflater.inflate(R.layout.item, parent, false);
        return new ViewHolder(v);
    }
    //5重写：给viewholder中的控件填充值,设置监听
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ArticleBean article = list.get(position);
        if (article == null)
            return;

        ViewHolder viewholder = (ViewHolder) holder;
        Picasso.with(context)
                .load("http://amicool.neusoft.edu.cn/Uploads/"+article.getThumb())
                .placeholder(R.drawable.f1)
                .into(viewholder.imageView);
        viewholder.tv_title.setText(article.getName());
        viewholder.tv_descript.setText(article.getDescription());
        viewholder.tv_time.setText(article.getUpdate_time());
        //条目中按钮监听事件
        viewholder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.remove(position);
                notifyDataSetChanged();
                //本例为测试，实际案例中删除条目应该会影响到数据库中
            }
        });
        //条目点击监听事件
        viewholder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取当前条目id
                String strid=String.valueOf(list.get(position).getId());
                Toast.makeText(context, strid, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public int getItemCount() {
        if(list==null)
            return 0;
        else
        return list.size();
    }
}
